//
//  ViewController.m
//  iFeller
//
//  Created by Wii on 15/12/10.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "WiiMenu.h"


@implementation WiiMenu

- (instancetype)initWiiMenuWithMenuButton:(UIButton *)menuButton menuItems:(NSMutableArray *)menuItems AddButton:(UIButton *)addButton{
    self = [super init];
    if (self) {
        self.menuButton = menuButton;
        self.menuItems = menuItems;
        self.addButton = addButton;
        self.status = 1;
        for (UIButton *button in menuItems) {
            button.hidden = YES;
        }
        [self.menuButton addTarget:self action:@selector(menuButtonTouchDown:withEvent:) forControlEvents:UIControlEventTouchDown];
        [self.menuButton addTarget:self action:@selector(dragMoving:withEvent: )
                  forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
        
        [self.menuButton addTarget:self action:@selector(dragEnded:withEvent: )
                  forControlEvents: UIControlEventTouchUpInside |
         UIControlEventTouchUpOutside];
        
    }
    return self;
}

- (void)menuButtonTouchDown:(UIControl *)c withEvent:event{
    NSLog(@"PopMenu: opened");
    if ([self.delegate respondsToSelector:@selector(menuPressed)]) {
        [self.delegate menuPressed];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.addButton.transform = CGAffineTransformMakeTranslation(0, 80);
    }];
    
    int i = 1;
    for(UIButton *itemButton in self.menuItems){
        [itemButton setHidden:NO];
        itemButton.transform = CGAffineTransformMakeScale(0.01, 0.01);
        [UIView animateWithDuration:0.7*i delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:0.3 options:0 animations:^{
            
            itemButton.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {
        }];
        i++;
    }
    
}

- (void)dragMoving:(UIControl *)c withEvent:event{
    
    NSSet* touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[self.menuButton superview]];
    if ([self.delegate respondsToSelector:@selector(fingerDragged:)]) {
        [self.delegate fingerDragged:currentPoint];
    }
    
    
    for(UIButton *itemButton in self.menuItems){
        if ( CGRectContainsPoint(itemButton.frame, currentPoint) ) {
            NSLog(@"PopMenu: dragging in %ld",(long)itemButton.tag);
            [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.2 options:0 animations:^{
                
                itemButton.transform = CGAffineTransformMakeScale(1.35, 1.35);;

            } completion:^(BOOL finished) {
              
            }];
            
        }
        else {
            [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
              
                itemButton.transform = CGAffineTransformIdentity;
                
            } completion:^(BOOL finished){
            
            }];
        }
    }
    
}

- (void) dragEnded:(UIControl *)c withEvent:event{
    
    NSSet* touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[self.menuButton superview]];
    BOOL menuItemSelected = false;
    for(UIButton *itemButton in self.menuItems){
        if ( CGRectContainsPoint(itemButton.frame, currentPoint) ) {
            NSLog(@"PopMenu: selected %ld",(long)itemButton.tag);
            menuItemSelected = true;
            [self.delegate menuItemsPressed:itemButton];
        }
        [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            itemButton.transform = CGAffineTransformMakeScale(0.01, 0.01);
            
        } completion:^(BOOL finished){
            [itemButton setHidden:YES];
            itemButton.transform = CGAffineTransformIdentity;
        }];
    }
    if (!menuItemSelected) {
        NSLog(@"PopMenu: cancelled");
        if (self.status == 1) {
            [UIView animateWithDuration:1 animations:^{
                self.addButton.transform = CGAffineTransformIdentity;
            }];
        }
        if ([self.delegate respondsToSelector:@selector(popMenuCancelled)]) {
            [self.delegate popMenuCancelled];
        }
    }
}


@end
